package util;

import common.Constant;
import net.minidev.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.flink.shaded.guava18.com.google.common.base.CaseFormat;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JdbcUtil {
    public static Connection getPhoenixConnection() {
        String driver = Constant.PHOENIX_DRIVER;
        String url = Constant.PHOENIX_URL;

        return getJdbcConnection(driver, url, null, null);
    }

    private static Connection getJdbcConnection(String driver,
                                                String url,
                                                String user,
                                                String password) {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("你提供的驱动类不存在: " + driver);
        }
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);

        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("请检测 url 是否正常, 用户名和密码是否正确: url=" + url + ", user=" + user + "password=" + password);
        }
        return conn;

    }


    public static void close(Connection conn) throws SQLException {
        if (conn != null && !conn.isClosed()) {
            conn.close();
        }
    }

    // select * from a where id=? and age=?;
    public static <T> List<T> queryList(Connection conn,
                                        String sql,
                                        Object[] args,  // 给 sql 中的占位符进行赋值. sql中有几个占位符,数组的长度就是几
                                        Class<T> tClass,
                                        boolean ... isUnderlineCamel) {
        boolean is = false;  // 默认不转
        if (isUnderlineCamel.length == 1) {
            is = isUnderlineCamel[0]; // 如果有值传入, 以传入的为准
        }

        List<T> result = new ArrayList<>();
        // 通用的 jdbc 查询工具
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            // 给占位符赋值
            for (int i = 0; args != null && i < args.length; i++) {
                Object arg = args[i];
                ps.setObject(i + 1, arg);
            }
//            ps.execute();  // 执行 ddl 和增删改
            ResultSet resultSet = ps.executeQuery(); // 结果集: 查询的结果
            ResultSetMetaData metaData = resultSet.getMetaData();
            while (resultSet.next()) { // 如果返回值是 true, 指针就移动到了这一行
                // 读取到一样数据. 数据的每一列全部取出, 封装到一个 T 类型的对象中
                // 如何 new 一个泛型对象. 调用的无参构造器
                T t = tClass.newInstance();
                // 把这一行所有的列读出来,然后封装到 t 对象中
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    // 列名: 元数据  表中的字段一般是用下划线命名, java的属性名一般驼峰
                    String name = metaData.getColumnLabel(i);
                    if (is) {
                        name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
                    }

                    // 列值: 从resultSet取
                    Object v = resultSet.getObject(i);

                    // 给 t 这个对下的 name 属性赋值为 v
                    BeanUtils.setProperty(t,name,v); // t.name=v
                }
                result.add(t);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public static Connection getMysqlConnection(){
        String driver = Constant.MYSQL_DRIVER;
        String url = Constant.MYSQL_URL;
        return getJdbcConnection(driver, url, "root", "aaaaaa");
    }

    public static Connection getMysqlConection() {
        String driver = Constant.MYSQL_DRIVER;
        String url = Constant.MYSQL_URL;
        return getJdbcConnection(driver, url, "root", "aaaaaa");
    }
}
